#!/usr/bin/env python
# -*- coding: utf-8 -*-

import apachelog, sys
from datetime import datetime, timedelta
from urlparse import urlparse

from django.db import IntegrityError
from django.core.management.base import BaseCommand
from django.conf import settings

from apachelog import apachelog
from core.models import Hit


class Command(BaseCommand):
    help = "Parse apache logs and store results in db"

    def handle(self, *args, **options):

        added_hits = 0
        
        p = apachelog.parser(settings.APACHE_LOG_FORMAT)

        for line in open(settings.APACHE_LOG_FILE_PATH):
            try:
                log_line = p.parse(line)
            except:
                sys.stderr.write("Unable to parse %s" % line)
            else:
                try:
                    hit = Hit()

                    method, url, protocol = log_line.get("%r").split()
                    scheme, netloc, path, params, qs, fragment = urlparse(url)
        
                    hit.url_path = path
                    hit.url_qs = qs
                    hit.method = method
                    hit.protocol = protocol
                    
                    hit.code = log_line.get("%>s")

                    time_string = log_line.get("%t")
                    hit.hit_at = datetime.strptime(
                        time_string, settings.APACHE_TIME_FORMAT)
                    hit.hit_at = hit.hit_at +\
                        timedelta(microseconds=int(log_line.get("%D", 0)))

                    size = log_line.get("%b")
                    try:
                        size = int(size)
                    except ValueError:
                        size = 0
                    hit.size = size
                    
                    hit.user = log_line.get("%u")
                    hit.from_ip = log_line.get("%h")
                    hit.url_referrer = log_line.get("%{Referer}i")
                    hit.user_agent = log_line.get("%{User-Agent}i")
                    
                    hit.save()
                    
                    added_hits += 1
                    
                except IntegrityError:
                    # the record alrady exists in db, don't save it
                    pass

        print "%d new hits added" % added_hits